
//
// 110402 - AH - (Zen) Created.
// 110403 - AH - Mime property and toString().
// 110411 - AH - getPitbulCode().
// 110412 - AH - void addOntologies(List<String>).
// 110413a- AH - Text property and constructor accepts String and not URL.
// 110413b- AH - toString() did not print ontology names, fixed.
// 110418 - AH - (Scotsman) Content(File, String).
// 110425 - AH - getOntologies() and getSpec().
// 110425 - AH - getOntology(String) and fixed getOntology(String).
// 110517 - AH - removeOntology(String).
// 110523 - AH - (Garage) Removed mime and url attributes, now content is just 
//               text and for which ontologies.
// 110722 - AH - (Garage) Removed not used constructor Content(File, String).
// 110724 - AH - (Bybanen *) setText(String).
//

package org.soekrat.engine.pit;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.soekrat.engine.ontology.OntologyDatastructure;
import org.soekrat.engine.util.Util;

/**
 * <p>
 *   Defines content for a node and which
 *   {@link OntologyDatastructure} it is defines in.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class Content 
{
  private String m_txt;
  
  private boolean m_note=false;
  
  private final Set<String> m_ontologies=new HashSet<String>();
  
  /**
   * <p>
   *   Constructor,
   * </p>
   * @param spec Specification for content, typical an URL.
   * @throws IllegalArgumentException If {@code txt} is the empty string. 
   */
  public Content(String spec){ this(spec, false); }
  
  /**
   * <p>
   *   Constructor,
   * </p>
   * @param spec Specification for content, typical an URL.
   * @param note {@code true} if is a note.
   * @throws IllegalArgumentException If {@code txt} is the empty string.  
   */
  public Content(String spec, boolean note){ m_note=note; setText(spec); }
  
  /**
   * <p>
   *   Tells if is note or not.
   * </p>
   * @return {@code true} and {@code false} if is not.
   */
  public boolean isNote(){ return m_note; }
  
  /**
   * <p>
   *   Gets text specifying content.
   * </p>
   * @return Text.
   */
  public String getText(){ return m_txt; }
  
  /**
   * <p>
   *   Sets text specifying content.
   * </p>
   * @param txt Text.
   * @throws IllegalArgumentException If {@code txt} is the empty string.
   */
  public void setText(String txt)
  {
    if (txt==null)
    {
      throw new NullPointerException("txt");
    }
    
    txt=txt.trim();
    if (txt.length()==0)
    {
      throw new IllegalArgumentException("txt is the empty String");
    }
    
    m_txt=txt;
  }
  
  /**
   * <p>
   *   Adds ontologies {@code this} exists in the context of.
   * </p>
   * @param ontologies Ontology names.
   */
  public void addOntologies(List<String> ontologies)
  {
    if (ontologies==null)
    {
      throw new NullPointerException("ontologies");
    }
    
    m_ontologies.addAll(ontologies);
  }
  
  /**
   * <p>
   *   Defines that {@code this} exists in the context of given ontology. 
   * </p>
   * @param ontology Ontology names.
   */
  public void addOntology(String ontology)
  {
    if (ontology==null)
    {
      throw new NullPointerException("ontology");
    }
    
    m_ontologies.add(ontology);
  }
  
  /**
   * <p>
   *   Defines that {@code this} not exists in the context of given ontology. 
   * </p>
   * @param ontology Ontology name.
   */
  public void removeOntology(String ontology)
  {
    if (ontology==null)
    {
      throw new NullPointerException("ontology");
    }
    
    m_ontologies.remove(ontology);
  }
  
  /**
   * <p>
   *   Removes
   *   {@link OntologyDatastructure}.
   * </p>
   * @param ontology {@link OntologyDatastructure} to remove.
   */
  public void removeOntology(OntologyDatastructure ontology)
  {
    if (ontology==null)
    {
      throw new NullPointerException("ontology");
    }
    
    m_ontologies.remove(ontology.getName());
  }
  
  /**
   * <p>
   *   Tells if {@code this} content is defined in given
   *   {@link OntologyDatastructure}.
   * </p>
   * @param ontology {@code Ontology} in question.
   * @return {@code true} if is, {@code false} if is not.
   */
  public boolean isDefinedFor(OntologyDatastructure ontology)
  {
    if (ontology==null)
    {
      throw new NullPointerException("ontology");
    }
    
    return m_ontologies.contains(ontology.getName());
  }
  
  /**
   * <p>
   *   Tells if {@code this} content is defined in given
   *   {@link OntologyDatastructure}.
   * </p>
   * @param ontology Name on ontology in question.
   * @return {@code true} if is, {@code false} if is not.
   */
  public boolean isDefinedFor(String ontology)
  {
    if (ontology==null)
    {
      throw new NullPointerException("ontology");
    }
    
    return m_ontologies.contains(ontology);
  }
  
  /**
   * <p>
   *   Gets names of ontologies {@code this} is defined for.
   * </p>
   * @return Names.
   */
  public String[] getOntologies()
  { 
    return m_ontologies.toArray(Util.E_STRING_A); 
  }
  
  /**
   * <p>
   *   Gets the part of Pitbul code to define content in a node form.
   * </p>
   * @return Code.
   */
  public String getPitbulCode()
  {
    StringBuilder sb=new StringBuilder();
    
    /*
    sb.append(" content").append(m_url);
    if (m_mime!=null)
    {
      sb.append(" mime-type ").append(m_mime);
    }
    
    if (m_ontologies.size()>0)
    {
      sb.append(" in ontology");
      for (String  curr : m_ontologies) 
      {
        sb.append(" ").append(curr);
      }
    }
    */
    
    return sb.toString();
  }
  
  @Override
  public String toString() 
  {
    StringBuilder sb=new StringBuilder();
    
    sb.append("text=").append(m_txt);
    if (!m_ontologies.isEmpty()) 
      sb.append(", ontologies=").append(m_ontologies);
    
    return sb.toString();
  }
  
  /**
   * <p>
   *   The empty array shared.
   * </p>
   */
  public static final Content[] EMPTY_ARRAY=new Content[0];
  
}
